import axios from '../axiosExtend.js'
import { shareUrl } from '../api.config.js';

class shareYouTube {
    constructor(obj, fn){
        this.share_params = obj;
        this.callback = fn;
        this.shareVideo();
    }
    shareVideo() {
        let _this = this;
        _shareVideo(_this.share_params).then(res => {
            console.log(res);
            let flag = false;
            let info = res.data.result;
            if(res.data.errorCode == '0x0') {
                $.message({message: "推流成功", type: "success"});
                flag = true;
            }else if(res.data.errorCode == '81300101'){
                $.message({message: "This R is not online", type: "info"});
                flag = false;
            }else if(res.data.errorCode == '81300102') {
                $.message({message: "R 没有开启 live encoder 模块", type: "info"});
                flag = false;
            }else if(res.data.errorCode == '81300103') {
                $.message({message: "创建encoder失败", type: "info"});
                flag = false;
            }else if(res.data.errorCode == '81300104') {
                $.message({message: "创建copier 失败", type: "info"});
                flag = false;
            }else if(res.data.errorCode == '81300105') {
                _this.handlePartSuccess(info);
            }else if(res.data.errorCode == '81300106') {
                $.message({message: "推流成功但是保存推流记录失败了，账号不存在", type: "info"});
                flag = false;
            }else if(res.data.errorCode == '81300107') {
                $.message({message: "推流成功但是保存推流记录失败了，未知异常", type: "info"});
                flag = false;
            }else if(res.data.errorCode == '81300108') {
                _this.handleAllFailed(info);
            }else if(res.data.errorCode == '813001010') {
                $.message({message: "去 Media 拿Rtmp全部失败", type: "info"});
                flag = false;
            }
            _this.callback(flag);
        });
    }
    handlePartSuccess(data) {
        let successInfo = data.success;
        let failedInfo = data.failed;
        $.message({message: "部分推流成功", type: "info"});
    }
    handleAllFailed(data) {
        $.message({message: "推流全部失败", type: "info"});
    }
}

function _shareVideo(params) {
    let url = shareUrl() + "youtube/shareVideo";
    return axios({
        url: url,
        data: params,
        method: 'post'
    });
}

export {
    shareYouTube
}